{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitc9425b4fd2d44566b8cda82b00a1f4f8",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import scipy.spatial.distance as ssd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "r1 = np.array([1,4,2,1])\n",
    "r2 = np.array([2,4,2,1])\n",
    "r3 = np.array([5,1,5,4])\n",
    "r4 = np.array([2,5,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sim(v1,v2):\n",
    "    v1_ = v1 - np.mean(v1)\n",
    "    v2_ = v2 - np.mean(v2)\n",
    "    return 1 - ssd.cosine(v1_, v2_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "(0.936585811581694, -0.8716019289105665, 0.7302967433402214)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim_2 = sim(r1,r2)\n",
    "sim_3 = sim(r1,r3)\n",
    "sim_4 = sim(r1,r4)\n",
    "(sim_2,sim_3, sim_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "4.047032197970447"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_rating = np.mean(r1) + (((5-np.mean(r2)) * sim_2)+ (2-np.mean(r3))*sim_3 + (5- np.mean(r4)) * sim_4) /sum(map(abs, (sim_2,sim_3, sim_4))) \n",
    "np.clip(predict_rating, 0,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}